//
//  SingleSelectionView.m
//  绿地管理系统
//
//  Created by rimi on 16/1/30.
//  Copyright © 2016年 hexs. All rights reserved.
//

#import "SingleSelectionView.h"

// 私有类
/**
 *  一个label，一个圈，它自己有一个点击事件，它有选中和非选中两个状态，选中 圈为实心 反之为空心
 */
@interface _SelectionView : UIControl

@property (nonatomic, strong) UIColor *tintColor;
@property (nonatomic, strong) UIView *circleView;
@property (nonatomic, strong) UILabel *textLabel;
//@property (nonatomic, assign) BOOL selected;

// 1. 点击这个控件，这个控件应该变为选中状态，而之前选中的那个控件应该变为非选中状态，所以控制某个控件选中状态的地方应该在用这个控件的地方而不是控件内部
// 2.
- (instancetype)initWithFrame:(CGRect)frame text:(NSString *)text;

@end

@implementation _SelectionView

- (instancetype)initWithFrame:(CGRect)frame text:(NSString *)text
{
    self = [super initWithFrame:frame];
    if (self) {
        CGFloat width = CGRectGetWidth(frame);
        CGFloat height = CGRectGetHeight(frame);
        self.textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width - height, height)];
        self.textLabel.text = text;
        self.textLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.textLabel];
        
        
        self.circleView = [[UIView alloc] initWithFrame:CGRectMake(width - height, 0, height, height)];
        self.circleView.backgroundColor = [UIColor clearColor];
        self.circleView.layer.borderColor = [UIColor blueColor].CGColor;
        self.circleView.layer.borderWidth = 1;
        [self addSubview:self.circleView];
    }
    return self;
}

- (void)setSelected:(BOOL)selected
{
    [super setSelected:selected];
//    _selected = selected;
    if (selected) {
        // 实心
        self.circleView.backgroundColor = self.tintColor;
    } else {
        // 空心
        self.circleView.backgroundColor = [UIColor clearColor];
    }
}

- (void)setTintColor:(UIColor *)tintColor
{
    _tintColor = tintColor;
    self.circleView.layer.borderColor = tintColor.CGColor;
    if (self.selected) {
        self.circleView.backgroundColor = tintColor;
    }
}

//- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//    
//}

@end

@interface SingleSelectionView ()

@property (nonatomic, strong) NSArray *selections;
@property (nonatomic, assign) NSInteger selectedIndex;

@end

@implementation SingleSelectionView

- (instancetype)initWithFrame:(CGRect)frame selectios:(NSArray *)selections
{
    self = [super initWithFrame:frame];
    if (self) {
        self.selections = selections;
        [self _initializeAppearance];
    }
    return self;             
}


- (void)_initializeAppearance
{
    [self.selections enumerateObjectsUsingBlock:^(NSString * obj, NSUInteger idx, BOOL * _Nonnull stop) {
        CGFloat height = CGRectGetHeight(self.frame);
        
        CGRect textRect = [obj boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading|NSStringDrawingUsesDeviceMetrics attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10]} context:nil];
        
        CGFloat unitWidth = CGRectGetWidth(textRect) +height;
        CGFloat width = CGRectGetWidth(self.frame);
        CGFloat interval = (width - unitWidth * self.selections.count)/(self.selections.count - 1);
        
        CGFloat x = idx * (unitWidth + interval);
        
        _SelectionView *view = [[_SelectionView alloc] initWithFrame:CGRectMake(x, 0, unitWidth, height) text:obj];
        view.tag = 100 + idx;
        [self addSubview:view];
    }];
}

- (void)onSelectionView:(_SelectionView *)sender
{
    // 1. 把之前选中的控件设为非选中
    _SelectionView *selectionView = (_SelectionView *)[self viewWithTag:100 + _selectedIndex];
    selectionView.selected = NO;
    
      //   2. 把现在选中这个设为选中
    sender.selected = YES;
    self.selectedIndex = sender.tag - 100;
}

- (void)setTintColor:(UIColor *)tintColor
{
    _tintColor = tintColor;
    for (int i = 0; i < self.selections.count; ++i) {
        _SelectionView *view = (_SelectionView *)[self viewWithTag:i + 100];
        view.tintColor = tintColor;
    }
}

@end

















